<?php

/**
 * This is the model class for table "allmenu".
 *
 * The followings are the available columns in table 'allmenu':
 * @property integer $id
 * @property string $menuname
 * @property string $menualias
 * @property string $description
 * @property integer $date
 */
class Allmenu extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Allmenu the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{allmenu}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('menuname, menualias', 'required'),
            array('menuname, menualias', 'unique'),
			array('menuname, menualias', 'length', 'max'=>50),
			array('description', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, menuname, menualias, date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'menu' => array(self::HAS_MANY, 'category', 'menu_id'),
        //    'links' => array(self::HAS_MANY, 'Linkmenu', 'menu_id'), 
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'menuname' => 'Название меню',
			'menualias' => 'Алиас меню',
			'description' => 'Описание',
			'date' => 'Дата и время создания',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('menuname',$this->menuname,true);
		$criteria->compare('menualias',$this->menualias,true);
//		$criteria->compare('description',$this->description,true);
//		$criteria->compare('date',$this->date);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    
    
   public static function allmenus()   //Функция возвращает список категорий (Применяется для связанных моделей)
    {
        $models = self::model()->findAll();
        $array = array();
            foreach($models as $item)
            {
                $array[$item->id] = $item->menuname;
            }
        return $array;   
    }
    
 
 /*   
    public static function showcategorys($menualias)
    {
        $categorys = array();
        $viewcats = array();                                                                         
        $categorys = self::model()->findByAttributes(array('menualias'=>$menualias));   //Находит категорию с заданным альясом
        
        foreach($categorys->menu as $item)            //$categorys->menu    menu -- имя связи в relations
         {
            if($item->status == 1) //Если категория опубликована, то заполнить массив информацией о категории
                 $viewcats[] = array('label'=>$item->title, 'url'=>'/index.php/site/index/cat/'.$item->url);   //Присваивает массиву категории в заданном меню
         }  
   
        return $viewcats;
    }
  */  
    
    
    public function beforeSave()  //Функция сохраняет время создания меню
    {
        if($this->isNewRecord)
            $this->date = time();
        
        return parent::beforeSave();
    }
    
    
    
    
}